package com.yh.tags;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * <pre>
 * - 有内容的标签
 * - 标签处理方法的返回常量：
 * - SKIP_BODY：跳过标签内容
 * - EVAL_BODY_INCLUDE：执行标签内容
 * - SKIP_PAGE：跳过标签后面的jsp内容
 * - EVAL_PAGE：继续执行标签后面的jsp内容
 * ClassName : com.yh.tags.ListTag
 * @author 湛锋
 * Date : 2020年8月10日 下午7:43:43
 * </pre>
 *
 */

public class ListTag extends TagSupport{
	
	private List<?> list;
	private String varIndex = "index";
	private String var = "vo";

	@Override
	public int doStartTag() throws JspException {
		if (list == null || list.size() == 0) {
			return SKIP_BODY;
		}
		int index = 0;
		pageContext.setAttribute(varIndex, index);
		pageContext.setAttribute(var, list.get(index));
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doAfterBody() throws JspException {
		int index = (Integer) pageContext.getAttribute(varIndex);
		++index;
		if (list.size() > index) {
			pageContext.setAttribute(varIndex, index);
			pageContext.setAttribute(var, list.get(index));
			return EVAL_BODY_AGAIN;
		} else {
			return SKIP_BODY;
		}
	}


	public void setList(List<?> list) {
		this.list = list;
	}

	public void setVarIndex(String varIndex) {
		this.varIndex = varIndex;
	}

	public void setVar(String var) {
		this.var = var;
	}
}
